﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Network.DLL
{
    /// <summary>
    /// 服务端处的客户端句柄
    /// </summary>
    [MultiThreadedSupportgAttribute( "当前的类所有成员都可锁定,支持多线程操作" )]
    public class ACEClientHandlerAtServer
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 服务端处的客户端句柄指针
        /// </summary>
        private IntPtr m_Value                                                  = IntPtr.Zero;
        /// <summary>
        /// 服务端处的客户端管理
        /// </summary>
        private ACESocketHandlerManagerAtServer m_SocketHandlerManager          = null;

        /// <summary>
        /// 处理数据回调函数
        /// </summary>
        internal DelegateClientHandlerProcessMessageBlock m_ProcessMessageBlock = null;
        /// <summary>
        /// 断开回调函数
        /// </summary>
        internal DelegateClientHandlerDisconnect m_Disconnect                   = null;
        #endregion

        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 初始化构造
        /// </summary>
        public ACEClientHandlerAtServer()
        {
            m_ProcessMessageBlock = new DelegateClientHandlerProcessMessageBlock( this.OnProcessMessageBlock );
            m_Disconnect = new DelegateClientHandlerDisconnect( this.OnDisconnect );
        }
        #endregion

        #region zh-CHS 共有属性 | en Public Properties
        /// <summary>
        /// 客户端的管理句柄
        /// </summary>
        public ACESocketHandlerManagerAtServer SocketHandlerManager
        {
            get { return m_SocketHandlerManager; }
            set { m_SocketHandlerManager = value; }
        }

        /// <summary>
        /// 是否在线
        /// </summary>
        public bool IsOnline
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_IsOnline(m_Value); }
        }

        /// <summary>
        /// 远程端口
        /// </summary>
        public int RemotePort
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_RemotePort(m_Value); }
        }

        /// <summary>
        /// 第一次数据包的时间
        /// </summary>
        public long FirstTime
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_FirstTime(m_Value); }
        }

        /// <summary>
        /// 远程的地址
        /// </summary>
        public string RemoteOnlyIP
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_RemoteOnlyIP(m_Value); }
        }

        /// <summary>
        /// 在线时间
        /// </summary>
        public long OnlineTime
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_OnlineTime(m_Value); }
        }

        /// <summary>
        /// 客户段的地址与端口
        /// </summary>
        public string ClientAddress
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_ClientAddress(m_Value); }
        }

        /// <summary>
        /// 消息包的总数量
        /// </summary>
        public long MessageBlockNumbers
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_MessageBlockNumbers(m_Value); }
        }

        /// <summary>
        /// 60秒内的数据包的数量
        /// </summary>
        public long MessageBlockNumbers60sec
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_MessageBlockNumbers60sec(m_Value); }
        }

        /// <summary>
        /// 最后数据包的时间
        /// </summary>
        public long LastMessageBlockTime
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_LastMessageBlockTime(m_Value); }
        }

        /// <summary>
        /// 数据包的处理时间
        /// </summary>
        public double MessageBlockProcessTime
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_MessageBlockProcessTime(m_Value); }
        }

        /// <summary>
        /// 数据包间隔的时间
        /// </summary>
        public long MessageBlockSpacingTime
        {
            get { return ACEClientHandlerAtServer.ACEClientHandlerAtServer_MessageBlockSpacingTime(m_Value); }
        }
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 关闭连接
        /// </summary>
        public void CloseSocket()
        {
            ACEClientHandlerAtServer.ACEClientHandlerAtServer_CloseSocket(m_Value);
        }

        /// <summary>
        /// 发送数据
        /// </summary>
        /// <param name="sendMessageBlock"></param>
        public void SendTo(ACEMessageBlock sendMessageBlock)
        {
            ACEClientHandlerAtServer.ACEClientHandlerAtServer_SendTo(sendMessageBlock.Value, m_Value);

            // 在ACESocketHandlerManagerAtServer.GetNewSendMessageBlock(...)处给出的,释放入内存池
            ACESocketHandlerManagerAtServer.s_InternalMessageBlockPool.ReleasePoolContent(sendMessageBlock);
        }
        #endregion

        #region zh-CHS 共有事件 | en Public Event
        /// <summary>
        /// 处理数据的事件
        /// </summary>
        public event EventDelegateClientHandlerProcessMessageBlock EventThreadProcessMessageBlock;
        /// <summary>
        /// 断开的事件
        /// </summary>
        public event EventDelegateClientHandlerDisconnect EventThreadDisconnect;
        #endregion

        #region zh-CHS 内部委托 | en Internal Delegate
        /// <summary>
        /// 处理数据的委托
        /// </summary>
        /// <param name="pRecvMessageBlock"></param>
        internal delegate void DelegateClientHandlerProcessMessageBlock(IntPtr pRecvMessageBlock);
        /// <summary>
        /// 断开的委托
        /// </summary>
        internal delegate void DelegateClientHandlerDisconnect();
        #endregion

        #region zh-CHS 私有的事件处理函数 | en Private Event Handlers
        #region zh-CHS 静态成员变量 | en Static　Member Variables
        /// <summary>
        /// 需要静态 否则在ACESocketMainServer初始化的时候会出现2048*2048的初始化
        /// 因为ACESocketMainServer初始化了2048个的ACEClientHandlerAtServer
        /// </summary>
        private static ACEProcessMessageBlockPool<ACEMessageBlock> s_MessageBlockPool = new ACEProcessMessageBlockPool<ACEMessageBlock>( "ACEClientHandlerAtServer - ACEMessageBlock", 1024 );
        #endregion
        /// <summary>
        /// 处理数据
        /// </summary>
        /// <param name="pRecvMessageBlock"></param>
        private void OnProcessMessageBlock(IntPtr pRecvMessageBlock)
        {
            if (EventThreadProcessMessageBlock != null)
            {
                // 从内存池获取内存 - 调用太频繁内存池优化
                ACEMessageBlock l_RecvMessageBlock = s_MessageBlockPool.AcquirePoolContent();
                if (l_RecvMessageBlock == null)
                {
                    Debug.WriteLine("ACEClientHandlerAtServer.OnProcessMessageBlock(...) - l_RecvMessageBlock == null error!");

                    return;
                }
                l_RecvMessageBlock.Value = pRecvMessageBlock;

                EventThreadProcessMessageBlock(l_RecvMessageBlock);

                // 释放入内存池
                s_MessageBlockPool.ReleasePoolContent(l_RecvMessageBlock);
            }
        }

        /// <summary>
        /// 断开
        /// </summary>
        private void OnDisconnect()
        {
            if (EventThreadProcessMessageBlock != null)
            {
                EventThreadDisconnect();
            }
        }
        #endregion

        #region zh-CHS 内部属性 | en Internal Properties
        /// <summary>
        /// 服务端处的客户端句柄指针
        /// </summary>
        protected internal IntPtr Value
        {
            get { return m_Value; }
            set { m_Value = value; }
        }
        #endregion

        #region zh-CHS 引入DLL接口 | en DLL Import
        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern bool ACEClientHandlerAtServer_IsOnline(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern int ACEClientHandlerAtServer_RemotePort(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ACEClientHandlerAtServer_FirstTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        private static extern string ACEClientHandlerAtServer_RemoteOnlyIP(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ACEClientHandlerAtServer_OnlineTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]
        private static extern string ACEClientHandlerAtServer_ClientAddress(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ACEClientHandlerAtServer_MessageBlockNumbers(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ACEClientHandlerAtServer_MessageBlockNumbers60sec(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ACEClientHandlerAtServer_LastMessageBlockTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern double ACEClientHandlerAtServer_MessageBlockProcessTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        /// <returns></returns>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern long ACEClientHandlerAtServer_MessageBlockSpacingTime(IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pSendMessageBlock"></param>
        /// <param name="pClientHandlerAtServer"></param>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void ACEClientHandlerAtServer_SendTo(IntPtr pSendMessageBlock, IntPtr pClientHandlerAtServer);

        /// <summary>
        /// dll 调用
        /// </summary>
        /// <param name="pClientHandlerAtServer"></param>
        [DllImport("Demo G.O.S.E.Net.dll", CallingConvention = CallingConvention.StdCall)]
        private static extern void ACEClientHandlerAtServer_CloseSocket(IntPtr pClientHandlerAtServer);
        #endregion
    }
}
#endregion

